package com.bodu.client.impl;

import cn.hutool.core.util.ObjectUtil;
import com.bodu.client.DepressCollectClient;
import com.bodu.core.entity.ParamReq;
import com.bodu.core.entity.enums.DelFlagEnum;
import com.bodu.core.entity.enums.StatusFlagEnum;
import com.bodu.core.entity.result.Result;
import com.bodu.depress.entity.vo.*;
import com.bodu.depress.service.*;
import com.bodu.depress.entity.DepressCollectDataVo;
import com.bodu.depress.entity.DepressQuestionDataVo;
import com.bodu.depress.entity.request.DepressCollectRequest;
import com.bodu.depress.entity.request.DepressCollectResultRequest;
import com.bodu.depress.entity.request.DepressCollectTypeRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("depressCollectClient")
public class DepressCollectClientImpl implements DepressCollectClient {
    private final DepressCollectTypeService depressCollectTypeService;
    private final DepressCollectService depressCollectService;
    private final DepressCollectDetailService depressCollectDetailService;
    private final DepressOptionsService depressOptionsService;
    private final DepressSolutionService depressSolutionService;
    private final DepressCollectResultService depressCollectResultService;

    @Autowired
    private DepressCollectStatissticsService depressCollectStatissticsService;

    public DepressCollectClientImpl(DepressCollectTypeService depressCollectTypeService, DepressCollectService depressCollectService, DepressCollectDetailService depressCollectDetailService, DepressOptionsService depressOptionsService, DepressSolutionService depressSolutionService, DepressCollectResultService depressCollectResultService) {
        this.depressCollectTypeService = depressCollectTypeService;
        this.depressCollectService = depressCollectService;
        this.depressCollectDetailService = depressCollectDetailService;
        this.depressOptionsService = depressOptionsService;
        this.depressSolutionService = depressSolutionService;
        this.depressCollectResultService = depressCollectResultService;
    }

    @Override
    public Result<List<DepressCollectDataVo>> findDepressCollects() {
        List<DepressCollectTypeVo> types = findUseDepressCollectTypes();
        List<DepressCollectDataVo> depressCollectDataVos = new ArrayList<>();
        for (DepressCollectTypeVo vo : types) {
            DepressCollectDataVo depressCollectDataVo = new DepressCollectDataVo();
            depressCollectDataVo.setCollecTypeId(vo.getId());
            depressCollectDataVo.setCollectTypeName(vo.getTypeName());
            List<DepressCollectVo> collects = findUseDepressCollects(vo.getId());
            depressCollectDataVo.setDepressCollects(collects);
            depressCollectDataVo.setCollectLength(collects.size());
            depressCollectDataVos.add(depressCollectDataVo);
        }
        return Result.success(depressCollectDataVos);
    }


    @Override
    public Result<List<DepressQuestionDataVo>> findDepressCollects(Long collectId) {
        List<DepressQuestionVo> list = depressCollectDetailService.findDepressQuestions(collectId);
        List<DepressQuestionDataVo> res = new ArrayList<>();
        for (DepressQuestionVo question : list) {
            DepressQuestionDataVo dqdv = new DepressQuestionDataVo();
            dqdv.setId(question.getId());
            dqdv.setTitle(question.getTitle());
            dqdv.setQuestionType(question.getQuestionType());
            dqdv.setQuestionTypeName(question.getQuestionTypeName());
            List<DepressOptionsVo> options = depressOptionsService.findOptionsByQuestionId(question.getId());
            dqdv.setOptions(options);

            DepressSolutionVo dv = depressSolutionService.getSolutionModelByQuestionId(question.getId());
            dqdv.setDepressSolutionVo(dv);
            res.add(dqdv);
        }

        return Result.success(res);
    }

    @Override
    public Result<DepressCollectResultVo> findDepressResult(DepressCollectResultRequest request) {
        try {
            DepressCollectResultVo dcrv = depressCollectResultService.findDepressResultByCollectIdAndScore(request.getCollectId(), request.getScore());
            return Result.success(dcrv);
        } catch (Exception e) {
            return Result.fail("查询失败！");
        }
    }

    @Override
    public Result<DepressCollectVo> findDepressCollect(Long id) {
        DepressCollectVo model = depressCollectService.getModel(id);
        model.setFirstWord(firstWord(model.getCollectTitle()));
        return Result.success("查询成功！", model);
    }

    private List<DepressCollectVo> findUseDepressCollects(Long collectTypeId) {
        ParamReq<DepressCollectRequest> paramReq = new ParamReq<>();
        paramReq.setCurrent(1);
        paramReq.setSize(1000);
        DepressCollectRequest depressCollectRequest = new DepressCollectRequest();
        depressCollectRequest.setCollectTypeId(collectTypeId);
        depressCollectRequest.setStatusFlag(StatusFlagEnum.USE.getValue());
        depressCollectRequest.setDelFlag(DelFlagEnum.NO.getValue());
        paramReq.setEntity(depressCollectRequest);
        List<DepressCollectVo> list =  depressCollectService.listPage(paramReq);
        list.forEach(l -> {
            l.setFirstWord(firstWord(l.getCollectTitle()));
            DepressCollectStatissticsVo depressCollectStatissticsVo = depressCollectStatissticsService.getDepressStatissticsByCollectId(l.getId());
            if (ObjectUtil.isNotEmpty(depressCollectStatissticsVo)) {
                l.setDepressExamCount(depressCollectStatissticsVo.getExamCount());
            } else {
                l.setDepressExamCount(0);
            }
        });

        return list;
    }

    private String firstWord(String title) {
        if(ObjectUtil.isEmpty(title)) {
            return "";
        }
        return title.substring(0,1);
    }

    /**
     * 得到有用的题集类别
     *
     * @return
     */
    private List<DepressCollectTypeVo> findUseDepressCollectTypes() {
        ParamReq<DepressCollectTypeRequest> param = new ParamReq<>();
        param.setCurrent(1);
        param.setSize(100);
        DepressCollectTypeRequest depressCollectTypeRequest = new DepressCollectTypeRequest();
        depressCollectTypeRequest.setStatusFlag(StatusFlagEnum.USE.getValue());
        depressCollectTypeRequest.setDelFlag(DelFlagEnum.NO.getValue());
        param.setEntity(depressCollectTypeRequest);
        return depressCollectTypeService.listPage(param);
    }
}
